/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package n1.dhcntt.k3;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import n1.dhcnttk3.n2013.NhanVien_Interface;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author Anguyen090
 */
public class NhanVien_Server extends UnicastRemoteObject implements NhanVien_Interface{
    
    public NhanVien_Server() throws RemoteException{
        super();
    }

    private ResultSet rs;
    private PreparedStatement rst;
        
    //ket noi csdl
    private Connection conn(){
        Connection cn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String database = "jdbc:mysql://localhost:3307/ql_banhang_sieuthi";
            String userName = "root";
            String password = "kiengiang";
            cn = DriverManager.getConnection(database, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cn;
    }
    
    public static void main(String[] arrgs){
        try {
            NhanVien_Server  rmiServer = new NhanVien_Server();
            LocateRegistry.createRegistry(9999);
            Registry registry = LocateRegistry.getRegistry(9999);
            registry.rebind("nv",rmiServer);
            System.out.println("Server Start...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
      
    
    @Override
    public AbstractTableModel loadData() throws RemoteException {
        TableModel rsTableModel = null;
        try {
            String sql = "SELECT * FROM nhanvien where isActive=true";
            rst=conn().prepareStatement(sql);
            rs=rst.executeQuery();
            rsTableModel = DbUtils.resultSetToTableModel(rs);
            
        }catch(Exception e) {
            
        }
        return (AbstractTableModel) rsTableModel;
    }

    @Override
    public boolean insertNV(String tenNV, String diaChi, int gioiTinh, int CMND, String soDT) throws RemoteException {
            boolean flag = false;
            try {
                Statement state = conn().createStatement();
                String sql = "INSERT INTO nhanvien(tenNV, diaChi, gioiTinh, CMND, soDT )values('"+tenNV+"', '"+diaChi+"', '"+gioiTinh+"','"+CMND+"','"+soDT+"')";
                int rskq = state.executeUpdate(sql);
                if(rskq == 1){
                    flag = true;
                }else{
                    flag = false;
                }
            }catch (Exception e) {
                flag = false;
            }
            return flag;
    }

    @Override
    public boolean UpdateNV(int ID, String tenNV, String diaChi, int gioiTinh, int CMND, String soDT) throws RemoteException {
        boolean flag=false;
        try {
            Statement state = conn().createStatement();
            String sql = "UPDATE nhanvien SET tenNV = '"+tenNV+"', diaChi='"+diaChi+"', gioiTinh='"+gioiTinh+"', CMND='"+CMND+"', soDT='"+soDT+"' WHERE ID = '"+ID+"'";
            int rs = state.executeUpdate(sql);
            if(rs==1){
                flag=true;
            }else{
                flag=false;
            }
        } catch (Exception e) {
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean DeleteNV(int ID, boolean isActive) throws RemoteException {
        boolean flag=false;
        try {
            Statement state = conn().createStatement();
            String sql = "UPDATE nhanvien SET isActive = '"+isActive+"' WHERE ID = '"+ID+"'";
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}
